package com.wwwarehouse.taskcenter.adapter.job_point;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.Button;
import android.widget.ImageView;

import com.wwwarehouse.common.adapter.media.CommonAdapter;
import com.wwwarehouse.common.adapter.media.ViewHolder;
import com.wwwarehouse.taskcenter.R;
import com.wwwarehouse.taskcenter.bean.job_point.JobPointSetRuleResponseBean;
import com.wwwarehouse.taskcenter.eventbus_event.JobPointStateEvent;

import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by Jackie on 2017/10/30.
 * 设置作业点规则 适配器
 */

public class RuleAdapter extends CommonAdapter<JobPointSetRuleResponseBean.JobPointRuleDetailsBean> {
    private int mItemHeight;

    public interface OnDetailsClickListener {
        void onDetailsClick(JobPointSetRuleResponseBean.JobPointRuleDetailsBean jobPointRuleDetailsBean);
    }

    private OnDetailsClickListener mOnDetailsClickListener;

    public void setOnDetailsClickListener(OnDetailsClickListener onDetailsClickListener) {
        this.mOnDetailsClickListener = onDetailsClickListener;
    }

    public RuleAdapter(Context context, int layoutId, List<JobPointSetRuleResponseBean.JobPointRuleDetailsBean> list, int itemHeight) {
        super(context, layoutId, list);

        this.mItemHeight = itemHeight;
    }

    @Override
    public void convert(ViewHolder holder, final JobPointSetRuleResponseBean.JobPointRuleDetailsBean jobPointRuleDetailsBean) {
        AbsListView.LayoutParams params = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        params.height = mItemHeight;
        holder.getConvertView().setLayoutParams(params);

        final ImageView selectImg = holder.getView(R.id.iv_select);
        Button detailsBtn = holder.getView(R.id.btn_details);

        holder.getConvertView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jobPointRuleDetailsBean.setSelect(!jobPointRuleDetailsBean.isSelect());

                notifyDataSetChanged();

                EventBus.getDefault().post(new JobPointStateEvent(jobPointRuleDetailsBean));
            }
        });

        detailsBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnDetailsClickListener != null) {
                    mOnDetailsClickListener.onDetailsClick(jobPointRuleDetailsBean);
                }
            }
        });

        holder.setText(R.id.tv_name, jobPointRuleDetailsBean.getTempInatanceName());

        if (jobPointRuleDetailsBean.isSelect()) {
            selectImg.setImageResource(R.drawable.category_select);
        } else {
            selectImg.setImageResource(R.drawable.category_no_select);
        }
    }
}
